package net.optix.ip.commands
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import net.optix.ip.business.*;
	import net.optix.ip.events.AddFieldActionEvent;
	import net.optix.ip.events.GetActionsEvent;
	import net.optix.ip.events.GetInventionEvent;
	import net.optix.ip.model.ModelLocator;

	public class AddFieldActionCommand implements Command, Responder
	{
		// attributes ============================
		private var model : ModelLocator = ModelLocator.getInstance();


		// functions ============================
		public function execute( event:CairngormEvent ) : void
		{
			trace("update action");
			var myEvent:AddFieldActionEvent = AddFieldActionEvent( event );
			
			var delegate:InventionDelegate = new InventionDelegate( );     
			delegate.addFieldAction( this , myEvent.vo );			
		}
		
		//----------------------------------------------------------------------------
		public function onResult( event : * = null ) : void
		{	
			trace(event.result+"  add field actin");
			try
			{
				var invention:XML			=	event.result;
				
				var	result:int				=	invention.result;
				switch(result)
				{
					case -2:
						model.result	=	"Access Denied .";
						break;
					case 0:
						model.result	=	"Program error!";
						break;
					case 1:
						model.result	=	"You have successfully recorded the patent application filing !";
						var getActions:GetActionsEvent	=	new GetActionsEvent(model.currentInvention.inventionID);
						com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getActions);
						model.close		=	"yes";
						
						var getInvention:GetInventionEvent	=	new GetInventionEvent(model.currentInvention.inventionID);
						CairngormEventDispatcher.getInstance().dispatchEvent(getInvention);
						break;
				}
			}
			catch(error:Error)
			{
				model.result	=	"Program error";
			}	
			model.bSubmit	=	true;
		}
		
		//----------------------------------------------------------------------------
		public function onFault( event : * = null ) : void
		{
			trace(event.fault);
			model.result	=	"Program error";
			model.bSubmit	=	true;
		}

	}
}